package ray.wx.servlet;

import ray.wx.util.*;
import ray.wx.struct.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter out = response.getWriter();
		
        String username = request.getParameter("username");  
        String password = request.getParameter("password");
        
        MysqlUtil mysql = new MysqlUtil(MysqlUtil.dbUrl, MysqlUtil.dbUser, MysqlUtil.dbPass);
        try {
			mysql.connect();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.print("database connect failed!");
		    out.close();  
		    out = null;	
		    return;
			//throw new ServletException("Database init failed!");
		}
        
        ResultSet result;
		String passwordFromDB = "";
		int userId = 0;
        String sql = "select user_id, password from mg_user where user_name = '" + username + "';";
        int count = 0;
		try {
			result = mysql.get_record_value(sql);
			while (result.next()){
				userId = result.getInt(1);
				passwordFromDB = result.getString(2);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.print("sql excute failed!");
		    out.close();  
		    out = null;	
		    return;
		}
		
		String errMsg = "";
		
        if (userId == 0){
        	errMsg = "username not found!";
        } else if (!password.equals(passwordFromDB)){
        	errMsg = "password not correct";
        } else {
            //login correct
            MgUserInfo userInfo = new MgUserInfo(userId, password, 1);
            HttpSession session = request.getSession();
            session.setAttribute(MgUserInfo.MG_USER_ATTR, userInfo);        	
        }
        
		out.print(errMsg);
	    out.close();  
	    out = null;
	    return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
